﻿## kill Main Install if Terminate = $true
if ($env:terminate -eq $true) {exit 0}

# Add Sharepoint pssnapin
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

# Set up a bunch of useful aliases...

Set-Alias psconfig ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\psconfig.exe")
Set-Alias stsadm ((Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\14.0').Location + "Bin\stsadm.exe")

$MyDir = Split-Path ($MyInvocation.MyCommand.Definition )


## adding this conditional for compatibility of current farms (Nate)
## checking for database prefix, if not do default
if ($env:DataBasePrefix -eq $null){
$ConfigDB = $env:configdb
$AdminContentDB = "$env:configdb_CA_Content"
}else{
$ConfigDB = $env:DataBasePrefix +'_Config'
$AdminContentDB = $env:DataBasePrefix +'_CA_Content'
}

Function Get-SPFarmExist() {
   trap [Exception]{
    Write-Host "Server is currently not connected to farm.. Continuing install"
    continue
   }


  return get-SPFarm
  
 
}


$farm = Get-SPFarmExist



	if ($farm -ne $null){
	Write-Host -ForegroundColor Yellow "Farm $env:farmname has already been created. You are connected to the $env:farmname Skipping running psconfig."
	exit 0
	}
	
$configDBtest = ./PsConfig/Execute-MSSQL-Command.ps1 "select * from sysdatabases where name = '$ConfigDB'" "$env:sqlserver" "master"
	if ($configDBtest -eq $null){

	Write-Host -ForegroundColor Yellow "Farm ConfigDB does not exist, creating farm..."
	$env:serverrole = "CA"

	} else{

	Write-Host -ForegroundColor Yellow "Farm ConfigDB does exist, attempting to join farm $env:farmname..."
	$env:serverrole = "WFE"


	}
## end MSIT




echo "Running PSConfig..."
echo "sqlserver : $env:sqlserver"



if( $env:ServerRole -eq "CA" )
{

	$servicepass = '"' + $env:serviceacctpwd + '"'
	echo "psconfig -cmd configdb -create -server $env:sqlserver -database $ConfigDB -user $env:serviceacct -password $servicepass -passphrase ""$env:farmpassphrase"" -admincontentdatabase $AdminContentDB -cmd helpcollections -installall -cmd secureresources -cmd services -install -cmd installfeatures -cmd adminvs -provision -port 8888 -windowsauthprovider onlyusentlm -cmd applicationcontent  -install -cmd installcheck -noinstallcheck"  #-cmd installhealthrules
	psconfig -cmd configdb -create -server $env:sqlserver -database $ConfigDB -user $env:serviceacct -password $servicepass -passphrase "$env:farmpassphrase" -admincontentdatabase $AdminContentDB -cmd helpcollections -installall -cmd secureresources -cmd services -install -cmd installfeatures -cmd adminvs -provision -port 8888 -windowsauthprovider onlyusentlm -cmd applicationcontent  -install -cmd installcheck -noinstallcheck  #-cmd installhealthrules
	if( $lastexitcode -ne 0)
	{
		Write-Error "PSconfig failed. error code $lastexitcode"	
		exit $lastexitcode;
	}
	else
	{
		Write-Host "PSConfig finished successfully."
	}

	echo "Creating new managed account..."
	$acct =  Get-SPManagedAccount | ? { $_.username -eq "$env:serviceacct" }
	if( $acct -eq $null )
	{
		New-SPManagedAccount (New-Object System.Management.Automation.PSCredential "$env:serviceacct", (ConvertTo-SecureString "$env:serviceacctpwd" -AsPlainText -Force))
		if( !$? )
		{
			echo "Failed to create farm admin account $env:serviceacct"
		}
	}

	$acct =  Get-SPManagedAccount | ? { $_.username -eq "$env:serviceacct" }
	if( $acct -eq $null )
	{
		New-SPManagedAccount (New-Object System.Management.Automation.PSCredential "$env:serviceacct", (ConvertTo-SecureString "$env:serviceacctpwd" -AsPlainText -Force))
		if( !$? )
		{
			echo "Failed to create service account $env:serviceacct"
		}
	}

}
else
{
	Write-Host "psconfig -cmd configdb -connect -server $env:sqlserver -database $ConfigDB -passphrase ""$env:farmpassphrase"" -cmd installCheck -cmd helpcollections -installall -cmd secureresources -cmd services -install -cmd installfeatures -cmd adminvs -provision -port 8888 -windowsauthprovider onlyusentlm -cmd applicationcontent -install"
	psconfig -cmd configdb -connect -server $env:sqlserver -database $ConfigDB -passphrase "$env:farmpassphrase" -cmd installCheck -cmd helpcollections -installall -cmd secureresources -cmd services -install -cmd installfeatures -cmd adminvs -provision -port 8888 -windowsauthprovider onlyusentlm -cmd applicationcontent -install
	if( $lastexitcode -ne 0)
	{
		Write-Error "PSconfig failed. error code $lastexitcode"
		$env:stop = $true
		exit $lastexitcode;
	}
	else
	{
		Write-Host "PSConfig finished successfully."
	}	
}



exit 0;

###########################################################################
#  
#   End
#
###########################################################################
